package com.lgd.factoryattendance.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lgd.factoryattendance.Bean.DebugLog;
import com.lgd.factoryattendance.Bean.ProjectDebugSpinner;
import com.lgd.factoryattendance.Bean.ProjectManagement;
import com.lgd.factoryattendance.Bean.ProjectSpinner;
import com.lgd.factoryattendance.R;
import com.lgd.factoryattendance.adapter.DebugLogAdapter;
import com.lgd.factoryattendance.adapter.DebugLogHistoryAdapter;
import com.lgd.factoryattendance.adapter.MyAdapter;
import com.lgd.factoryattendance.httpService.API;
import com.lgd.factoryattendance.httpService.httpPort.IStringResponse;

import java.util.ArrayList;
import java.util.List;

public class DebugLogHistoryActivity extends AppCompatActivity implements View.OnClickListener{
    Long deviceid;
    private ArrayList<ProjectDebugSpinner> projectDebugSpinners = null;
//    Spinner projectDebug_SP;
    List<String> aCodeLogList = new ArrayList<>();
    List<String> nameLogList = new ArrayList<>();
    List<String> debugContentList = new ArrayList<>();
    List<String> debugTimeList = new ArrayList<>();
    List<String> projectNameList = new ArrayList<>();
    List<List<String>> logListItem = null;
    ListView lv_debuglog;
    LinearLayout layout_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debug_log_history);
        init();
    }
    public void init(){
        layout_back = findViewById(R.id.layout_back);
        layout_back.setOnClickListener(this);
        lv_debuglog = findViewById(R.id.lv_debuglog);
        SharedPreferences sharedPreferences = getSharedPreferences("device", MODE_PRIVATE);
        deviceid = sharedPreferences.getLong("deviceid",0);
//        ProjectManagement p = new ProjectManagement();
//        p.setDeviceid(deviceid);
//        findProjectName(p);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                DebugLogHistoryAdapter debugLogHistoryAdapter = new DebugLogHistoryAdapter(projectDebugSpinners, DebugLogHistoryActivity.this);//实例化适配器
//                projectDebug_SP.setAdapter(debugLogHistoryAdapter);
//                projectDebug_SP.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//                    @Override
//                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                        clearLogList();  // 每次请求清空列表
//                        DebugLog debugLog = new DebugLog();
//                        debugLog.setProjectid(projectDebugSpinners.get(position).getProjectId());
//                        selectDebugLog(debugLog);
//                        new Handler().postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                logListView();
//                            }
//                        },400);
//                    }
//                    @Override
//                    public void onNothingSelected(AdapterView<?> parent) {
//                    }
//                });
//            }
//        },400);
        Intent intent = getIntent();
        Long userid = intent.getLongExtra("userId",0);
        System.out.println(userid+"-----------------------");
        DebugLog debugLog = new DebugLog();
        debugLog.setUserid(userid);
        selectDebugLog(debugLog);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                logListView();
            }
        },400);
    }
    //  项目 下拉框列表数据
    public void findProjectName(ProjectManagement p){
        API.findProjectName(p, new IStringResponse() {
            @Override
            public void onSuccess(String result) {
                Looper.prepare();
                JSONObject j = JSONObject.parseObject(result);
                List<ProjectManagement> projectManagements = new Gson().fromJson(j.getString("data"), new TypeToken<List<ProjectManagement>>(){}.getType() );
                projectDebugSpinners = new ArrayList<>();
                if (projectManagements != null && projectManagements.size() >0 ){
                    for (int i = 0; i < projectManagements.size(); i++) {
                        projectDebugSpinners.add(new ProjectDebugSpinner(
                                projectManagements.get(i).getProductacode(),
                                projectManagements.get(i).getProjectname(),
                                projectManagements.get(i).getProjectid()));
                    }
                }
                Looper.loop();
            }
            @Override
            public void onFail(int statusCode) {}
        });
    }
    // 调试日志列表
    public void selectDebugLog(DebugLog debugLog){
        API.selectDebugLog(debugLog, new IStringResponse() {
            @Override
            public void onSuccess(String result) {
                DebugLogHistoryActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject j = JSONObject.parseObject(result);
                        List<DebugLog> debugLogList = new Gson().fromJson(j.getString("data"), new TypeToken<List<DebugLog>>(){}.getType() );
                        System.out.println(debugLogList+"---日志 条数----");
                        if (debugLogList.size() > 0 ){
                            for (int i = 0; i < debugLogList.size(); i++) {
                                aCodeLogList.add(debugLogList.get(i).getProductacode());
                                nameLogList.add(debugLogList.get(i).getNickName());
                                debugContentList.add(debugLogList.get(i).getDebugcontent());
                                debugTimeList.add(debugLogList.get(i).getDebugtime());
                                projectNameList.add(debugLogList.get(i).getProjectname());
                            }
                        }
                    }
                });

            }
            @Override
            public void onFail(int statusCode) {}
        });
    }

    // 列表 实例化
    public void logListView(){
        logListItem = new ArrayList<>();
        for (int i = 0; i < aCodeLogList.size(); i++) {
            List<String> logShowItem = new ArrayList<>();
            logShowItem.add(aCodeLogList.get(i));
            logShowItem.add(projectNameList.get(i));
            logShowItem.add(debugContentList.get(i));
            logShowItem.add(debugTimeList.get(i));
            logShowItem.add(nameLogList.get(i));
            logListItem.add(logShowItem);
        }
        DebugLogAdapter adapter = new DebugLogAdapter(DebugLogHistoryActivity.this,logListItem);

        lv_debuglog.setAdapter(adapter);
    }
    //  清空列表数据
    public void clearLogList(){
        if (logListItem != null && logListItem.size() > 0){
            logListItem.clear();
            aCodeLogList.clear();
            nameLogList.clear();
            debugContentList.clear();
            debugTimeList.clear();
            projectNameList.clear();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearLogList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_back:
                finish();
                break;
        }
    }
}